package com.atguigu.gmall.common.config.interceptor;

import com.atguigu.gmall.common.util.RequestUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Component
public class MyHeaderInterceptor implements RequestInterceptor {


    @Override
    public void apply(RequestTemplate requestTemplate) {
        //同一个线程获取request
        HttpServletRequest request = RequestUtil.getRequestForThread();
        if (request == null) {
            return ;
        }


        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            if ("cookie".equalsIgnoreCase(key) ||
                    "token".equalsIgnoreCase(key) || "userTempId".equalsIgnoreCase(key)) {
                requestTemplate.header(key, request.getHeader(key));
            }
        }
    }


}
